//
//  YHWebViewController.m
//  FAKER
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHWebViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "BaseClassWebView.h"
#import "BaseClassImageView.h"

@interface YHWebViewController ()<UIWebViewDelegate>
@property(nonatomic, strong)BaseClassWebView *webView;
@property(nonatomic, strong)YHBottomView *bottomView;
@property(nonatomic, strong)YHTopView *topView;
@property(nonatomic, strong)YHLoadingView *loadingView;
@end

@implementation YHWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //点击底部按钮方法
    [self clickBottomButtonAction];
    
    //下拉刷新
    _webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_webURLString]]];
    }];
    
    //修改时间栏字体颜色为黑色
//    if (NSUSERDEFAULTS_NIGHT) {
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
//    } else {
//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
//    }
    [UIApplication sharedApplication].statusBarStyle = NSUSERDEFAULTS_NIGHT;
    
    //创建加载中视图
    _loadingView = [[YHLoadingView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, SCREEN_HEIGHT - 80 - 40)];
    [self.view addSubview:_loadingView];
}

#pragma mark - 布局
- (void)loadView
{
    [super loadView];
    //创建顶部视图
    _topView = [[YHTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80) YHSelected:_yhSelected];
    [self.view addSubview:_topView];
    
    //创建webView
    _webView = [[BaseClassWebView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, SCREEN_HEIGHT - 80 - 40)];
    _webView.delegate = self;
    [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_webURLString]]];
    [self.view addSubview:_webView];
    
    //创建底部按钮
    _bottomView = [[YHBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    [self.view addSubview:_bottomView];
    
    //轻扫手势
//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];
//    swipe.direction = UISwipeGestureRecognizerDirectionRight;
//    [_webView addGestureRecognizer:swipe];
}

//#pragma mark - 轻扫方法
//- (void)swipeAction
//{
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

#pragma mark - 点击方法
#pragma mark 点击底部按钮
- (void)clickBottomButtonAction
{
    void (^bottomButtonBlock)(NSString *) = ^(NSString *buttonTitle) {
        if ([buttonTitle isEqualToString:@"back"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            [_webView.scrollView.mj_header beginRefreshing];
        }
    };
    _bottomView.block = bottomButtonBlock;
}

#pragma mark - webView协议方法
#pragma mark 点击图片
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    NSLog(@"%@", request.URL.absoluteString);
    if ([request.URL.absoluteString isEqualToString:@"http://www.myzaker.com/m/"]) {
        return NO;
    }
    return YES;
}
#pragma mark 已经开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    if (NSUSERDEFAULTS_NIGHT) {
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
    }
}
#pragma mark 加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //移除加载中视图
    [_loadingView removeFromSuperview];
    
    [_webView.scrollView.mj_header endRefreshing];
    if (NSUSERDEFAULTS_NIGHT) {
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
    }
}
//- (void)webViewDidStartLoad:(UIWebView *)webView
//{
//    if (NSUSERDEFAULTS_NIGHT) {
//        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
//        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
//    }
//}

#pragma mark - 修改时间栏字体颜色为白色
- (void)viewWillDisappear:(BOOL)animated
{
    //修改时间栏字体颜色为白色
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated
{
    [_loadingView createRotationAnimation];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
